/**
 * 此自定应用户模块为后台数据层categoriesModel模块，专门负责后端的categories数据库的数据处理
 * 
 */
// 引入MySQL模块
const mysql=require('mysql');
// 创建连接对象
const connection=mysql.createConnection({
    host:'127.0.0.1',
    port:'3306',
    user:'root',
    password:'root',
    database:'albx33'
})
module.exports={
    // 加载分类页面所有虽有分类数据
    getCategoriesAllData(callback){
        let sql='SELECT * FROM categories where isDelete=0';
        connection.query(sql,(err,result)=>{
            // console.log(result);
            callback(err,result);
        });
    },
    // 写入分类数据
    updateNewCategories(data,callback){
        let sql=`insert into categories set slug="${data.slug}",\`name\`="${data.name}",classname="${data.classname}"`;
        connection.query(sql,(err,result)=>{
            callback(err,result);
        })
    },
    // 查找分类数据
    findCategories(insertId,callback){
        // console.log(insertId);
        let sql=`select * from categories where id=${insertId}`;
        connection.query(sql,(err,result)=>{
            callback(err,result);
        });
    },
    // 根据id软删除数据库对应数据
    deleteCategories(id,callback){
        let sql=`update categories set isDelete=1 where id=${id}`;
        connection.query(sql,(err,result)=>{
            callback(err,result);
        })
    },
    // 根据id查找对应数据并进行修改
    editCategories(data,callback){
        let sql=`UPDATE categories set \`name\`='${data.name}',slug='${data.slug}',classname='${data.classname}' WHERE id=${data.id}`;
        // console.log(sql);
        connection.query(sql,(err,result)=>{
            callback(err,result);
        })
    },
    // 根据id数查找数据并删除
    deleteCategoriesByIdArr(ids,callback){
        let sql=`delete from categories where id in (${ids})`;
        connection.query(sql,(err,result)=>{
            callback(err,result);
        })
    }
}